package com.meng.baselibrary.fragment;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/5/11.
 */

public abstract class BaseFragment extends Fragment{
    protected View contentView;
    protected Context context;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(getContentViewLayout(),null);
        initView();
        return contentView;
    }
    protected abstract int getContentViewLayout();
    protected <T extends View> T findViewById(int id){
        return (T) contentView.findViewById(id);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    protected abstract void initView();
    protected abstract void initData();

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        onFragmentVisibleChange(isVisibleToUser);
    }

    protected void onFragmentVisibleChange(boolean isVisible){

    }
    /**
     * 获取状态栏高度
     */
    public int getStatusHeight() {
        final Rect rect = new Rect();
        getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int n = rect.top;
        if (n != 0) {
            return n;
        }
        try {
            final Class<?> forName = Class.forName("com.android.internal.R$dimen");
            n = getResources().getDimensionPixelSize(Integer.parseInt(forName.getField("status_bar_height").get(forName.newInstance()).toString()));
            return n;
        }catch (Exception ex) {
            return n;
        }
    }

    protected void initPaddingTob(){
        contentView.setPadding(contentView.getPaddingLeft()
        ,contentView.getPaddingTop()+getStatusHeight(),
                contentView.getPaddingRight(),
                contentView.getPaddingBottom());
        contentView.requestLayout();
    }
}
